
#ifndef   __HPLC_PROXY_H__
#define   __HPLC_PROXY_H__

#include <string>
#include <list>
#include <iostream>
#include <memory>

#include "../../common/include/IChannel.h"
#include "../../common/include/CThreadNotify.h"
#include "../../common/include/CMosquittoManager.h"
#include "../../common/include/CConfigManager.h"
#include "HPLCPacketManager.h"

class HPLCProxy : public IChannel
{

public:
    HPLCProxy(int freqPt) noexcept;
    ~HPLCProxy();

   
    int  connect(const char *host, int port, const char * username, const char *password, int keepalive = 5000);

    int  add_hplcarchives(std::list<std::string> lstDevAdds, uint8_t* rsp, int& rsplen);
    int  slavenode_ctrl(const std::string& slaveAddr, uint8_t *sendBuf, int buf_len, uint8_t* rsp, int& rsplen);
    int  clear_hplcarchives(uint8_t* rsp, int& rsplen);

    int  send_msg(uint8_t *req, int req_len, uint8_t *rep, int &rep_len, uint32_t timeout = 5000);
    

    CThreadNotify  m_thread_notify;
protected:
    static int  recv_msg_callback(CALLBACK_TYPE cb_type, void *data, int data_len, int result, void *arg);

private:
    int         m_freqPt;
    int64_t     m_req_id;
    bool        m_is_receiving;
    int         m_rsp_len;
    uint8_t*    m_rsp;

    

    std::string  m_reqtoken;
    std::string  m_rsptoken;
    uint64_t     m_send_timestamp;

    HPLCPacketManager m_hpm;
    std::shared_ptr<CMosquittoManager>      m_mosquitto_manager;


    void init_mqtt();
    void wait_recv_finished();
    void finish_signal();
    int  subscribe_topic();
    int  parse_rsptopic(std::string  topic,std::string  payload);

    void hplc_freqset();
    void OutputDecodedata(char* pData, int len);
};

#endif